<template>
  <el-dialog
    :title="!dataForm.id? '新增' : !disabled? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

      <el-form-item label="分类" prop="categoryIds">
        <el-select multiple clearable filterable collapse-tags v-model="dataForm.categoryIds" :disabled="disabled" placeholder="请选择"
                   class="width185">
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属支行" prop="branchIds">
        <el-select
          class="width100"
          v-model="dataForm.branchIds"
          filterable

          multiple
          reserve-keyword
          placeholder="请输入关键词"
          :loading="branchLoading">
          <el-option
            v-for="item in branchOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="活动隐藏 " prop="hide">
        <el-radio-group v-model="dataForm.hide" :disabled="disabled">
          <el-radio :label="1">不隐藏</el-radio>
          <el-radio :label="2">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="热门活动简称" prop="abbreviation">
        <el-input maxlength="10" v-model="dataForm.abbreviation" :disabled="disabled" placeholder="热门活动简称" ></el-input>
        <h5 style="color:red">（简称不要超过4个字）</h5>
      </el-form-item>

      <el-form-item label="活动标识" prop="sign">
        <el-radio-group v-model="dataForm.sign" :disabled="disabled">
          <el-radio :label="1">热门</el-radio>
          <el-radio :label="2">推荐</el-radio>

        </el-radio-group>
      </el-form-item>

      <el-form-item label="封面图片" prop="cover">
        <el-img v-model="dataForm.cover" :disabled="disabled">
        </el-img>
      </el-form-item>
      <el-form-item label="排序字段" prop="sort">
        <el-input v-model="dataForm.sort" :disabled="disabled" placeholder="排序字段"></el-input>
      </el-form-item>

      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" :disabled="disabled" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="类型 " prop="status">
        <el-radio-group v-model="dataForm.status" :disabled="disabled">
          <el-radio :label="1">上架</el-radio>
          <el-radio :label="2">下架</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="外链地址" prop="link">
        <el-input v-model="dataForm.link" :disabled="disabled" placeholder="外链地址"></el-input>
      </el-form-item>

<!--      <el-form-item label="封面" prop="cover">-->
<!--        <el-img v-model="dataForm.cover" :disabled="disabled">-->
<!--        </el-img>-->
<!--      </el-form-item>-->


      <el-form-item label="备注" prop="remarks">
        <el-input v-model="dataForm.remarks" :disabled="disabled" placeholder="备注"></el-input>
      </el-form-item>

      <el-form-item label="活动日历简介" prop="introduce">
        <el-input v-model="dataForm.introduce" :disabled="disabled" placeholder="活动日历简介"></el-input>
      </el-form-item>
      <el-form-item label="活动日历详情" prop="content">
        <ueditor v-model="dataForm.content" :disabled="disabled" placeholder="活动日历详情"></ueditor>
      </el-form-item>
      <el-form-item label="活动开始时间" prop="startTime">
        <el-date-picker type="datetime"  v-model="dataForm.startTime" :disabled="disabled"
                        value-format="yyyy-MM-dd" format="yyyy-MM-dd" placeholder="活动开始时间" ></el-date-picker>
      </el-form-item>
      <el-form-item label="活动结束时间" prop="endTime">
        <el-date-picker type="datetime"  v-model="dataForm.endTime" :disabled="disabled"
                        format="yyyy-MM-dd"   value-format="yyyy-MM-dd" placeholder="活动结束时间" ></el-date-picker>
      </el-form-item>

      <el-form-item label="时间连续 " prop="continuity">
        <el-radio-group v-model="dataForm.continuity" :disabled="disabled">
          <el-radio :label="1">连续</el-radio>
          <el-radio :label="2">不连续</el-radio>
        </el-radio-group>
      </el-form-item>



      <el-form-item v-if="dataForm.continuity === 2" label="每月" >
        <el-checkbox-group v-model="dateList" >
          <el-checkbox v-for="item in dayOption" :key="item" :label="item" :disabled="disabled" >
            {{item}}号
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item v-if="dataForm.continuity === 2" label="每星期" >
        <el-checkbox-group v-model="weekList" >
          <el-checkbox v-for="item in weekOption" :key="item" :label="item" :disabled="disabled" >
            周{{item}}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item v-if="dataForm.continuity === 2" label="法定节假日" prop="holiday">
        <el-radio-group v-model="dataForm.holiday" :disabled="disabled">
          <el-radio :label="1">不包括</el-radio>
          <el-radio :label="2">包括</el-radio>
        </el-radio-group>
      </el-form-item>




    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="quxiao()">取消</el-button>
      <el-button v-if="!disabled && !dataForm.ids" type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-button v-if="!disabled && dataForm.ids" type="primary" @click="updateDataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    var validateDeclared = (rule,value,callback) => {
      if (Date.parse(this.dataForm.startTime) > Date.parse(this.dataForm.endTime)) {
        callback(new Error("结束时间要大于开始时间"));
      } else {
        callback();
      }
    };
    return {
      disabled: false,
      visible: false,
      categoryList: [],
      dayOption: [],
      weekOption: [],
      weekList: [],
      branchOptions: [],
      dateList: [],
      branchLoading: true,
      dataForm: {
        id: 0,
        categoryIds: [],
        branchIds:[],
        days: '',
        weeks: '',
        holiday: '',
        sort: '',
        title: '',
        status: '',
        link: '',
        cover: '',
        remarks: '',
        introduce: '',
        content: '',
        startTime: '',
        endTime: '',
        dateRule: '',
        hide: '',
        abbreviation: '',
        sign: '',
        continuity: '',
        createTime: '',
        updateTime: ''},
      dataRule: {
        categoryIds: [
          {required: true, message: '分类不能为空', trigger: 'blur'}
        ],
        title: [
          {required: true, message: '标题不能为空', trigger: 'blur'}
        ],
        endTime: [
          {required: true, message: '结束时间不能为空', trigger: 'blur'},
          { validator: validateDeclared, trigger: 'change' }
        ],
        startTime: [
          {required: true, message: '开始时间不能为空', trigger: 'blur'},
          { validator: validateDeclared, trigger: 'change' }
        ]
      }
    }
  },
  activated () {
  },
  methods: {
    init (id, disabled) {
      this.disabled = disabled
      this.dataForm.id = id || ''
      this.dataForm.ids = null
      this.visible = true
      if (this.dayOption.length === 0){
        for (var i=1;i<=31;i++) {
          this.dayOption.push(i+"")
        }
      }
      if (this.weekOption.length === 0){
        for (var i=1;i<=7;i++) {
          this.weekOption.push(i+"")
        }
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: `/event/calendar/info/${this.dataForm.id}`,
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm = data.eventCalendar
              this.dateList = (data.eventCalendar.days || "").split(',')
              this.weekList = (data.eventCalendar.weeks || "").split(',')
              console.log(this.dateList)
              console.log(this.weekList)
            }
          })
        }
      })
      this.getCategoryList()
      this.getBranchList()
    },
    // 表单提交
    dataFormSubmit () {
      if (this.dateList.length != 0){
        this.dataForm.days=this.dateList.join(',')
      }
      if (this.weekList.length != 0){
        // console.log("测试周："+this.weekList)
        this.dataForm.weeks=this.weekList.join(',')
      }else {
        this.dataForm.weeks=""
      }
      this.$refs['dataForm']
        .validate((valid) => {
          if (valid) {
            this.$http({
              url: `/event/calendar/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: this.dataForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
    },

    quxiao(){
      this.visible = false
    },
    getCategoryList () {
      this.$http({
        url: `/event/category/queryAll`,
        method: 'get',
        params: {
          'status': 1
        },
        data: this.dataForm
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.categoryList = data.list
        }
      })
    },
    getBranchList (){
      this.$http({
        url: '/mall/branch/queryAll',
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.branchOptions = data.list
        }
      })
    },
    remoteMethod(query) {
      if (query !== '') {
        this.branchLoading = true;
        this.$http({
          url: '/mall/branch/queryAll',
          method: 'get',
          params: {
            'name': query
          }
        }).then(({data}) => {
          this.branchLoading = false;
          if (data && data.code === 0) {
            this.branchOptions = data.list
          }
        })
      } else {
        this.branchOptions = [];
      }
    }
  }
}
</script>
